<?php
	class CommentAction extends BaseAction{
		public function index()
		{
			$comment = M("Comment");
			import("ORG.Util.Page");
			$count = $comment->count();
			$page = new Page($count,c('PAGESIZE'));
			$show = $page->show();
			$data = $comment->limit($page->firstRow.','.$page->listRows)
							->join('zgcms_user on uid = zgcms_user.id')
							->join('zgcms_article on aid = zgcms_article.id')
							->field("zgcms_comment.content complete,zgcms_comment.content content,zgcms_comment.id id,user,aid,zgcms_comment.state state,title")
							->select();
			getSubStr($data,'content',30,'utf-8');
			getSubStr($data, 'title', 10, 'utf-8');
// 			echo $comment->getLastSql();
// 			dump($data);
			$this->assign("list",$data);
			$this->assign('page',$show);
			$this->display();
		}


		//通过审核
		public function pass(){
			if(empty($_GET['id'])){
				$this->error('请选择要审核的评论！');
				exit();
			}
			$comment = M('Comment');
			$comData = $comment->getById($_GET['id']);
			if(!$comData){
				$this->error('请确保要审核的评论ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 1;
			if($comment->where('id = '.$_GET['id'])->save($data)){
				$this->success('审核成功！');
			}else{
				$this->error('审核失败！');
			}
			
		}
		
		//取消审核
		public function cancel(){
			if(empty($_GET['id'])){
				$this->error('请选择要取消审核的评论！');
				exit();
			}
			$comment = M('Comment');
			$comData = $comment->getById($_GET['id']);
			if(!$comData){
				$this->error('请确保要取消审核的评论ID正确！');
				exit();
			}
			$data = array();
			$data['state'] = 0;
			if($comment->where('id = '.$_GET['id'])->save($data)){
				$this->success('取消审核成功！');
			}else{
				$this->error('取消审核失败！');
			}
		}
		
		//批量审核
		public function check(){
// 			dump($_POST);
			$comment = M('Comment');
			$data = array();
			$_effectRows = 0;//记录修改影响行数
			for($i = 0; $i < count($_POST['ids']); $i++)
			{
				
				if(!is_numeric($_POST['states'][$i])){
					$this->error('请确保输入的是数字！');
					exit();
				}
				$data['state'] = $_POST['states'][$i] > 0 ? 1 : 0;
				$result = $comment->where('id = '.$_POST['ids'][$i])->save($data);
				$_effectRows += $result == false ? 0 : $result;
			}
			if(0 == $_effectRows){
				$this->error('批审失败或没有需要审核的评论！');
			}else{
				$this->success('批审成功！');
			}
		}
		
		public function delete()
		{
			if(!empty($_GET['id'])){
				$comment = M("Comment");
				if($comment->delete($_GET['id'])){
				$this->success("删除评论成功!");
			}else{
			$this->error("删除评论失败!");
			}
			}else{
			$this->error("请选择要删除的评论！");
			}
		}
	}
?>